import React, {Component} from 'react';
import './index.less';
import {base, toast, high, promptBox} from 'nc-lightapp-front';
import templatePrint from "../../container/functions/templatePrint";
import ReferSearch from "../../../../../../hrpub/common/components/referSearch/org";
import printOutput from "../../container/functions/printOutput";
import setQueryMeta from "../../container/functions/setQueryMeta";

const {PrintOutput} = high;
const {NCCheckbox, NCTabs} = base;
const NCTabPane = NCTabs.NCTabPane;

class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {printData: {}};
        this.tabChange = this.tabChange.bind(this);
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    tabChange(value) {
        if (this.props.disabledAll) return;
        this.props.updateQueryModel(value);
    }

    onChangeHandle(type) {
        return (value) => {
            this.props.updateState({
                [type]: value,
                pageInfo: {
                    ...this.props.pageInfo,
                    pageIndex: 1
                }
            }, () => {
                if (this.props.hasSearched) {
                    this.props.getMainTableData();
                }
            });
        }
    }

    onBtnOperation(props, btncode, areacode, opt) {
        switch (btncode) {
            case 'add':
                this.props.updateState({
                    addNewModalVisible: true
                });
                break;
            case 'query':
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('query', true);
                break;
            case 'sort':
                this.props.updateState({
                    sortModalVisible: true
                });
                break;
            case 'refresh':
                if (this.props.hasSearched) {
                    this.props.updateState({
                        pageInfo: {
                            ...this.props.pageInfo,
                            pageIndex: 1
                        }
                    }, () => {
                        this.props.getMainTableData();
                    });
                }
                break;
            case 'batchedit':
                const checkedRows = this.props.table.getCheckedRows('info');
                if (checkedRows.length === 0) {
                    toast({color: "warning", content: this.props.json['hi6007-000111']});
                    /* 国际化处理： 请勾选要批改的数据*/
                } else {
                    this.props.updateState({
                        batchEditModalVisible: true
                    });
                }
                break;
            case 'print':
                templatePrint.call(this);
                break;
            case 'output':
                printOutput.call(this);
                break;
            default:
                break;
        }
    }

    render() {
        const {button, isIncHisPartJob, orgVal, queryModel, disabledAll} = this.props;
        const tabs = [{
            key: '1',
            text: this.props.json['hi6007-000109']/* 国际化处理： 本组织人员兼职*/
        }, {
            key: '2',
            text: this.props.json['hi6007-000110']/* 国际化处理： 非本组织人员兼职*/
        }];
        return (
            <div>
                <div className="header">
                    <div className="header-refer">
                        <ReferSearch
                            disabled={disabledAll}
                            orgVal={orgVal}
                            getOrgData={this.updateOrgValue}
                        />
                    </div>
                    <div className="header-checkbox-group">
                        <NCCheckbox
                            checked={isIncHisPartJob}
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('isIncHisPartJob')}
                            disabled={disabledAll}
                        >
                            {this.props.json['hi6007-000112']}{/* 国际化处理： 包含历史兼职人员*/}
                        </NCCheckbox>
                    </div>

                    <div className="btn-group">
                        <div className="definition-icons">
                            {button.createButtonApp({
                                area: 'head',
                                onButtonClick: (props, btncode) => {
                                    this.onBtnOperation(props, btncode, 'head', this);
                                }
                            })}
                        </div>
                    </div>
                </div>
                <div className='header-tabs'>
                    <NCTabs
                        navtype="turn"
                        contenttype="moveleft"
                        tabBarPosition="top"
                        activeKey={queryModel}
                        onChange={this.tabChange}
                    >
                        {
                            tabs.map(item =>
                                <NCTabPane tab={item.text} key={item.key}/>)
                        }
                    </NCTabs>
                </div>
                <PrintOutput
                    ref='printOutput'
                    url={'/nccloud/hrtrn/pub/RecordsPrintAction.do'}
                    data={this.state.printData}
                />
            </div>
        );
    }
}

export default Header;
